package com.coolwen.controller;

import com.coolwen.entity.Member;
import com.coolwen.entity.SccocerException;
import com.coolwen.entity.User;
import com.coolwen.service.MemberService;
import com.coolwen.service.UserTeamService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created by 松红 on 2016/12/5.
 */
@SessionAttributes("team")
@Controller
@RequestMapping(value = {"/team/member"})
public class MemberController {

    /**
     * 引用logj文件，进行日志打印
     */
    private static final Logger LOGGER = Logger.getLogger(MemberController.class);
    /**
     * 引入member，userTeam的service，方便调用方法
     */
    @Autowired
    MemberService memberService;

    @Autowired
    UserTeamService userTeamrService;

    /**
     * 通过球员所在队伍的id进行查询队员信息，防止查询到其他队伍的队员信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "list")
    public String listALLMember(Model model, HttpSession session) {
        /*通过user的session，读取teamId*/
        User user = (User) session.getAttribute("loginUser");
        int teamId = userTeamrService.findUserTeamByUserID(user.getUserId()).getTeamId();
        /*通过findMemberByTeamId方法，找到该球队的所有球队的球员信息*/
        model.addAttribute("members", memberService.findMembersByTeamId(teamId));

        return "team/member";
    }

    /**
     * 通过选取的球员id进行更详细的数据获取
     *
     * @param memberId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{memberId}")
    public String listMember(@PathVariable int memberId, Model model) {
        /*通过该球员的id,获取该球员的详细数据*/
        model.addAttribute("member", memberService.findMemberById(memberId));
        return "team/singlemember";
    }

    /**
     * 通过选取的球员id进行数据删除
     *
     * @param memberId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{memberId}/delete")
    public String deletemember(@PathVariable int memberId, Model model) {
        /*对选取后的单个球员信息，进行更多的操作，（返回，删除，查看）*/
        model.addAttribute(memberService.deleteMemberById(memberId));
        return "redirect:/team/member/list";
    }


    /**
     * 如果addmember页面post请求，则访问以下代码
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/addmember", method = RequestMethod.GET)
    public String addmember(Model model) {
        model.addAttribute(new Member());
        return "team/addmember";
    }

    /**
     * 如果addmember页面是get请求，则访问以下代码
     *
     * @param member
     * @return
     */
    @RequestMapping(value = "/addmember", method = RequestMethod.POST)
    public String addmember(@Validated Member member, HttpSession session) {
        /*通过user的session，读取teamId*/
        User user = (User) session.getAttribute("loginUser");
        int teamId = userTeamrService.findUserTeamByUserID(user.getUserId()).getTeamId();
        if(member==null){
            throw new SccocerException("球员信息不能为空！");
        }
        /*实现添加球员的操作，并返回到球员列表界面*/
        member.setMemberTeam(teamId);
        memberService.addmember(member);
        return "redirect:/team/member/list";

    }

}
